package org.andromeo;

import java.util.List;

import org.andromeo.connectionapi.LogoutProcessor;
import org.andromeo.prefs.PrefsActivity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TablistActivity extends TabActivity {

	ListView listview = null;

	private List<User> userlist;
	private GRSession grsession;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tablist);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec friendlistSpec; // Resusable TabSpec for each tab

		// Create Intents to launch tab content (eq activities)
		Intent intent = new Intent().setClass(this, FriendlistActivity.class);
		Intent msgintent = new Intent().setClass(this,
				MessagelistActivity.class);
		Intent sentmsgintent = new Intent().setClass(this,
				SentMessagelistActivity.class);
		Intent visitorIntent = new Intent().setClass(this,
				VisitorlistActivity.class);

		/**
		 * Initialize a TabSpec for each tab and add it to the TabHost
		 */
		TabSpec messagetabSpec = tabHost.newTabSpec("messages");
		messagetabSpec = messagetabSpec.setIndicator(getResources().getString(
				R.string.messagesTab), getResources().getDrawable(
				R.drawable.ar_tab_msg_active));
		messagetabSpec = messagetabSpec.setContent(msgintent);
		tabHost.addTab(messagetabSpec);

		TabSpec sentmessagesSpec = tabHost.newTabSpec("sentmessages");
		sentmessagesSpec.setIndicator(getResources().getString(
				R.string.sentmessagesTab), getResources().getDrawable(
				R.drawable.ar_tab_snd_active));
		sentmessagesSpec.setContent(sentmsgintent);
		tabHost.addTab(sentmessagesSpec);

		friendlistSpec = tabHost.newTabSpec("friendlist");
		friendlistSpec.setIndicator(getResources().getString(
				R.string.friendlistTab), getResources().getDrawable(
				R.drawable.ar_tab_usr_active));
		friendlistSpec.setContent(intent);
		tabHost.addTab(friendlistSpec);

		TabSpec visitorlistSpec = tabHost.newTabSpec("visitors");
		visitorlistSpec.setIndicator(getResources().getString(
				R.string.visitorTab),getResources().getDrawable(R.drawable.ar_tab_vis_active));
		visitorlistSpec.setContent(visitorIntent);
		tabHost.addTab(visitorlistSpec);

		tabHost.setCurrentTab(0);

		grsession = ((ARApplication) getApplication()).getGrsession();

	}

	/**
	 * creates menu
	 * 
	 * @author fabian
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_about:
			try {
				startActivity(new Intent(TablistActivity.this,
						AboutActivity.class));
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		case R.id.menu_logout:
			/**
			 * cheap logout mechanism
			 */
			stopService(new Intent(this, HeartbeatService.class));
			try {
				startService(new Intent(getApplicationContext(),
						HeartbeatService.class).setAction(HeartbeatService.STOP_HEARTBEAT));
				stopService(new Intent(getApplicationContext(),
						HeartbeatService.class).setAction(HeartbeatService.STOP_HEARTBEAT));
				LogoutProcessor.sendLogoutRequest(grsession);
				grsession.setSessionStatus(GRSession.STATUS_OK_OFFLINE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			finish();
			break;
		case R.id.menu_preferences:
			startActivity(new Intent(TablistActivity.this,
					PrefsActivity.class));
			break;
		}
		return true;
	}
}
